<template>
  <div class="create">
    <h2 class="margin-bom title">{{title}}</h2>

    <div class="manageFrom">
      <!-- 表单校验 -->
      <el-form :model="manages" :rules="rules" ref="manages" status-icon>
        <el-form-item label="账号" prop="name">
          <el-input type="text" v-model="manages.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="manages.pass" :disabled="manages.id != ''"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass" v-if="manages.id == ''">
          <el-input type="password" v-model="manages.checkPass"></el-input>
        </el-form-item>
        <el-form-item label="属性">
          <el-input type="text" v-model="manages.prop"></el-input>
        </el-form-item>
        <el-form-item label="时间" prop="time">
          <el-date-picker v-model="manages.time" value-format="yyyy-MM-dd"  type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="createManage()" v-if="manages.id == ''">添加</el-button>
          <el-button type="success" @click="updateManage()" v-if="manages.id != ''">修改</el-button>
          <el-button @click="back()">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import ajax from "../../common/js/ajax";
import API from "../../common/js/api";
import rels from "../../common/js/manageRules";
export default {
  data() {
    return {
      title: "",
      manages: {
        name: "",
        pass: "",
        checkPass: "",
        prop: "",
        time: "",
        id: ""
      },
      // 校验规则
      rules: rels(this)
    };
  },
  methods: {
    createManage() {
      // 校验表单
      this.$refs.manages.validate(valid => {
        if (valid) {
          ajax(
            API.addManage,
            "get",
            this.manages,
            d => {
              this.$message({
                message: "添加成功",
                type: "success"
              });
              this.$router.replace("/index/manageList");
            },
            err => {
              this.$message({
                message: err.data.info,
                type: "warning"
              });
            }
          );
        } else {
          this.$message({
            message: "请填写字段",
            type: "warning"
          });
        }
      });
    },
    back() {
      this.$router.go(-1);
    },
    updateManage() {
      this.$refs.manages.validate(valid => {
        delete this.manages._id;
        ajax(
          API.updateManage,
          "get",
          this.manages,
          d => {
            this.$message({
              message: "修改成功",
              type: "success"
            });
            this.$router.replace("/index/manageList");
          },
          err => {
            this.$message({
              message: err.data.info,
              type: "warning"
            });
          }
        );
      });
    }
  },
  watch: {},
  computed: {},
  mounted() {
    let len = Object.keys(this.$route.query);
    if (len.length == 0) {
      this.title = "添加管理员";
    } else {
      this.title = "编辑管理员";
      this.manages.id = this.$route.query.id;
      ajax(
        API.findManage,
        "get",
        { id: this.manages.id },
        d => {
          this.manages = d.data.data[0];
        },
        err => {
          alert(err.data.info);
        }
      );
    }
  }
};
</script>
<style lang="stylus" scoped>
@import '../../common/css/index.styl';

.el-col {
  line-height: 40px;
}

.create >>> .el-row {
  min-width: 700px;
}

.create >>> .el-input {
  float: right;
  width: 89%;
}

.create >>> .el-form-item__error {
  left: 100px;
}

.el-date-editor--date {
  margin-left: 40px !important;
  float: left !important;
  width: 30% !important;
}

.manageFrom {
  width: 60%;
  margin: 0 auto;
  min-width: 700px;
}
</style>